package courseSystem.servlet;

import courseSystem.bean.UserBean;
import courseSystem.data.DB;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class DoorServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        switch (req.getParameter("flag")) {
            case "login":
                userLogin(req, resp);
                break;
            case "logout":
                userLogout(req, resp);
                break;
            case "signup":
                studentSignup(req, resp);
                break;
            case "change":
                changePassword(req, resp);
                break;
            default:
                resp.sendRedirect("index.jsp");
                break;
        }
    }

    private void userLogout(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getSession().removeAttribute("user");
        req.getSession().removeAttribute("allCourses");
        req.getSession().removeAttribute("myCourses");
        req.getSession().removeAttribute("keyWord");
        req.getSession().removeAttribute("findCourses");
        req.getSession().removeAttribute("url");
        req.getSession().setAttribute("prompt", "退出成功");
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
    private void userLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        String userClass = req.getParameter("userClass");
        List<HashMap<String, String>> list = new DB().query("*", userClass, "id='" + id + "' and password='" + password + "'");
        if (!list.isEmpty()) {
            req.getSession().setAttribute("user", new UserBean(list.get(0)));
            req.getSession().setAttribute("url", "jsp/"+userClass+"Home.jsp");
            req.getSession().setAttribute("prompt", "欢迎您，"+list.get(0).get("name"));
            req.getRequestDispatcher("courseServlet?flag=all").forward(req, resp);
        }else{
            req.getSession().setAttribute("prompt", "用户名或密码错误");
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }
    private void studentSignup(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HashMap<String, String> s = new HashMap<>();
        s.put("id", (int)(Math.random()*1000000000)+"");
        s.put("nickname", req.getParameter("nickname"));
        s.put("password", req.getParameter("psw"));
        s.put("name", req.getParameter("name"));
        s.put("classroom", req.getParameter("classroom"));
        s.put("department",req.getParameter("department"));
        if(new DB().insert("student",s.keySet().toArray(new String[0]), s.values().toArray(new String[0]))){
            req.getSession().setAttribute("user", new UserBean(s));
            req.getSession().setAttribute("url", "studentHome.jsp");
            req.getSession().setAttribute("prompt", "注册成功！您的账号为："+s.get("id"));
            req.getRequestDispatcher("courseServlet?flag=all").forward(req, resp);
        }else {
            req.getSession().setAttribute("prompt", "注册失败");
            req.getRequestDispatcher("jsp/signup.jsp").forward(req, resp);
        }
    }
    private void changePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserBean user = (UserBean) req.getSession().getAttribute("user");
        String pass1=req.getParameter("pass1");
        String pass2=req.getParameter("pass2");
        String pass3=req.getParameter("pass3");
        String userClass=req.getParameter("userClass");
        if(pass1.equals(user.getUser().get("password"))){
            if(pass2.equals(pass3)){
                if(new DB().update(userClass,new String[]{"password"},new String[]{pass2},"id='"+user.getUser().get("id")+"'")){
                    user.getUser().put("password",pass2);
                    req.getSession().setAttribute("user",user);
                    req.getSession().setAttribute("prompt","修改成功");
                }else req.getSession().setAttribute("prompt","更新异常");
            }else{
                req.getSession().setAttribute("prompt","两次密码不一致");
            }
        }else req.getSession().setAttribute("prompt","原密码错误");
        req.getRequestDispatcher("jsp/"+userClass+"Home.jsp").forward(req, resp);
    }
}
